package com.mini.game.global;

import com.mini.game.auth.ThreadLocalUserContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author 小鱼儿
 * @date 2020/9/21 9:41
 */
@Aspect
@Order(999)
@Component
public class TimeCostAspect {
    private final static Logger logger = LoggerFactory.getLogger(TimeCostAspect.class);

    @Pointcut("execution(public * com.mini.game.api.ApiController.*(..))")
    public void dsPointCut() {

    }

    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long last = System.currentTimeMillis();
        Object obj = point.proceed();
        long userId = ThreadLocalUserContext.getUserId();
        long cost = System.currentTimeMillis() - last;
        if (cost > 10) {
            logger.info("userId={} method={} 耗时={}ms", userId, point.getSignature().getName(), cost);
        }
        return obj;
    }
}
